package com.yanqu.road.dao.impl.activity.hongxian;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.hongxian.CrossHongXianNoticeData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossHongXianNoticeDataDaoImpl extends TempDataObjectDao<CrossHongXianNoticeData> {


    public static String getCrossHongXianNoticeDataSql = "select * from `t_c_hong_xian_notice` where `activityId`=? order by getTime desc limit ? ";
    public List<CrossHongXianNoticeData> getCrossHongXianNoticeData(int activityId, int limit) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<CrossHongXianNoticeData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossHongXianNoticeDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, limit);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossHongXianNoticeData data = parse(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateCrossHongXianNoticeDataSql = "update `t_c_hong_xian_notice` set userId=?,serverId=?,nickName=?,reward=?,getTime=?,param=? where activityId=?";
    @Override
    protected boolean updateData(CrossHongXianNoticeData crossHongXianNoticeData) {
        crossHongXianNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossHongXianNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHongXianNoticeData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHongXianNoticeData.getNickName()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHongXianNoticeData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHongXianNoticeData.getGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHongXianNoticeData.getParam()));
        para.put(i++, new DbParameter(Types.INTEGER, crossHongXianNoticeData.getActivityId()));
        if (getDb().executeNoneQuery(updateCrossHongXianNoticeDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossHongXianNoticeDataSql = "insert into `t_c_hong_xian_notice`(activityId,userId,serverId,nickName,reward,getTime,param) values (?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(CrossHongXianNoticeData crossHongXianNoticeData) {
        crossHongXianNoticeData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossHongXianNoticeData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHongXianNoticeData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHongXianNoticeData.getServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHongXianNoticeData.getNickName()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHongXianNoticeData.getReward()));
        para.put(i++, new DbParameter(Types.BIGINT, crossHongXianNoticeData.getGetTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, crossHongXianNoticeData.getParam()));
        if (getDb().executeNoneQuery(insertCrossHongXianNoticeDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossHongXianNoticeData parseData(ResultSet rs) throws SQLException {
        CrossHongXianNoticeData data = new CrossHongXianNoticeData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setServerId(rs.getLong("serverId"));
        data.setNickName(rs.getString("nickName"));
        data.setReward(rs.getString("reward"));
        data.setGetTime(rs.getLong("getTime"));
        data.setParam(rs.getString("param"));

        return data;
    }
}
